<template>
  <div class="app-container">
    <el-row :gutter="10" v-if="optType !='view'" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSelectItem"
          v-hasPermi="['mes:qc:qctemplate:add']"
        >新增产品/物料</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddType"
          v-hasPermi="['mes:qc:qctemplate:add']"
        >新增分类</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:qc:qctemplate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:qc:qctemplate:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="templateproductList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" v-if="optType!='view'"  width="55" align="center" />
      <el-table-column label="产品物料编码" align="center" width="100px" prop="itemCode" :show-overflow-tooltip="true"/>
      <el-table-column label="产品物料名称" align="center" width="120px" prop="itemName" :show-overflow-tooltip="true"/>
      <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
      <el-table-column label="单位" align="center" width="100px" prop="unitOfMeasure" />
<!--      <el-table-column label="最低检测数" align="center" width="150px" prop="quantityCheck" >
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.quantityCheck" size="mini" />
        </template>
      </el-table-column>
      <el-table-column label="最大不合格数" align="center" width="150" prop="quantityUnqualified" >
        <template slot-scope="scope">
          <el-input-number  v-model="scope.row.quantityUnqualified" size="mini" />
        </template>
      </el-table-column>-->

      <el-table-column label="操作"  align="center" v-if="optType != 'view'" width="120px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:qc:qctemplate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <ItemSelect ref="itemSelect" @onSelected="onItemSelected" ></ItemSelect>

  </div>
</template>

<script>
import {
  listTemplateproduct,
  getTemplateproduct,
  delTemplateproduct,
  addTemplateproduct,
  updateTemplateproduct,
  addTemplateproductType
} from "@/api/mes/qc/templateproduct";
import ItemSelect  from "@/components/itemSelect/index.vue";
import Treeselect from "@riophae/vue-treeselect";
import { treeselect } from "@/api/mes/md/itemtype";
import Template from "@/views/print/printtemplate/list.vue";
export default {
  name: "Templateproduct",
  components: {Template, Treeselect, ItemSelect},
  props:{
      optType: null,
      templateId: null,
      qcType: null
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检测模板-产品表格数据
      templateproductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dialogType: '',
      itemTypeOptions: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateId: this.templateId,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantityCheck: null,
        quantityUnqualified: null,
        crRate: null,
        majRate: null,
        minRate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateId: [
          { required: true, message: "检测模板ID不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "产品物料不能为空", trigger: "blur" }
        ],
        itemCode: [
          { required: true, message: "产品物料不能为空", trigger: "blur" }
        ],
        itemTypeId: [
          { required: true, message: "产品物料分类不能为空", trigger: "blur" }
        ],
      }
    };
  },

  watch: {
    // 监听templateId变化
    templateId: function (newVal, oldVal) {
      this.templateproductList =[];
        this.queryParams.templateId = newVal;
      this.getList();
    }
  },
  methods: {
    /** 查询检测模板-产品列表 */
    getList() {
      if(this.templateId!=null) {
        this.loading = true;
        listTemplateproduct(this.queryParams).then(response => {
          this.templateproductList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        templateId: this.templateId,
        attr1: this.qcType,
        itemId: null,
        itemCode: null,
        itemName: null,
        itemTypeId: null,
        specification: null,
        unitOfMeasure: null,
        quantityCheck: null,
        quantityUnqualified: null,
        crRate: 0,
        majRate: 0,
        minRate: 100,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.dialogType = 'product'; // 设置产品标识
      this.open = true;
      this.title = "添加检测模板-产品/物料";
    },
    handleAddType(){
      this.reset();
      this.dialogType = 'category'; // 设置分类标识
      this.getTreeselect();
      this.open = true;
      this.title = "添加检测模板-分类";
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      treeselect().then(response => {
        this.itemTypeOptions = response.data;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getTemplateproduct(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检测模板-产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateTemplateproduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if(this.dialogType === 'product'){
              addTemplateproduct(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }else{
              addTemplateproductType(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }

          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let that=this;
      this.$modal.confirm('是否确认删除物料产品"' + row.itemName + '"？').then(function() {
        that.templateproductList.splice(that.templateproductList.indexOf(row),1);
      });
    },
    //物料选择弹出框
    handleSelectItem(){
      this.$refs.itemSelect.showFlag = true;
    },
    //物料选择弹出框
    onItemSelected(rows){
      if(rows.length > 0){
        let t=rows.filter(item=>!this.templateproductList.find(productItem=>productItem.itemCode === item.itemCode))
        if(t.length>0) {
          this.templateproductList.push(...t)
          /*this.templateproductList.forEach(item => {
            this.$set(item, 'quantityCheck', 1);
            this.$set(item, 'quantityUnqualified', 0);
          })*/
        }
      }

      console.log(this.templateproductList)
    },
  }
};
</script>
